//package com.swancloud.connectors.jersey2;
//
//import javax.ws.rs.ClientErrorException;
//import javax.ws.rs.ProcessingException;
//import javax.ws.rs.core.Response;
//
//import com.swancloud.api.exceptions.ConnectionException;
//import com.swancloud.api.exceptions.ResponseException;
//import com.swancloud.core.transport.ClientConstants;
//import com.swancloud.core.transport.HttpExecutorService;
//import com.swancloud.core.transport.HttpRequest;
//import com.swancloud.core.transport.HttpResponse;
//import com.swancloud.openstack.internal.OSAuthenticator;
//import com.swancloud.openstack.internal.OSClientSession;
//
///**
// * HttpExecutor is the default implementation for HttpExecutorService which is responsible for interfacing with Jersey and mapping common status codes, requests and responses
// * back to the common API
// *
// * @author Jeremy Unruh
// */
//public class HttpExecutorServiceImpl implements HttpExecutorService {
//
//    private static final String NAME = "Jersey 2 Connector";
//
//
//    /**
//     * {@inheritDoc}
//     */
//    @Override
//    public <R> HttpResponse execute(HttpRequest<R> request) {
//        try {
//            return invoke(request);
//        } catch (ResponseException re) {
//            throw re;
//        } catch (Exception e) {
//            throw new ConnectionException("Error during execution: " + e, 0, e);
//        }
//    }
//
//    /**
//     * Invokes the given request
//     *
//     * @param <R> the return type
//     * @param request the request to invoke
//     * @return the response
//     * @throws Exception the exception
//     */
//    private <R> HttpResponse invoke(HttpRequest<R> request) throws Exception {
//
//        HttpCommand<R> command = HttpCommand.create(request);
//
//        try {
//            return invokeRequest(command);
//        } catch (ProcessingException pe) {
//            throw new ConnectionException(pe.getMessage(), 0, pe);
//        } catch (ClientErrorException e) {
//            throw ResponseException.mapException(e.getResponse().getStatusInfo().toString(), e.getResponse().getStatus(), e);
//        }
//    }
//
//    private <R> HttpResponse invokeRequest(HttpCommand<R> command) throws Exception {
//        Response response = command.execute();
//        if (command.getRetries() == 0 && response.getStatus() == 401 && !command.getRequest().getHeaders().containsKey(ClientConstants.HEADER_OS4J_AUTH)) {
//            OSAuthenticator.reAuthenticate();
//            command.getRequest().getHeaders().put(ClientConstants.HEADER_X_AUTH_TOKEN, OSClientSession.getCurrent().getTokenId());
//            return invokeRequest(command.incrementRetriesAndReturn());
//        }
//        return HttpResponseImpl.wrap(response);
//    }
//
//    @Override
//    public String getExecutorDisplayName() {
//        return NAME;
//    }
//
//}
